<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="150px" label="标题">
        <template slot-scope="{row}">
          <router-link :to="'/article/cate/edit/'+row.id" class="link-type">
            <span>{{ row.title }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column min-width="200px" label="说明">
        <template slot-scope="{row}">
          <router-link :to="'/article/cate/edit/'+row.id" class="link-type">
            <span>{{ row.instr }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" label="排序">
        <template slot-scope="{row}">
          <router-link :to="'/article/cate/edit/'+row.id" class="link-type">
            <span>{{ row.sort }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" label="是否显示">
        <template slot-scope="{row}">
          <el-switch
            v-model="row.hidden"
            :active-value="0"
            :inactive-value="1"
            @change="switchChange($event, row)"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.addTime | parseTimeFormat('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <router-link :to="'/article/cate/edit/'+scope.row.id">
            <el-button type="primary" size="small" icon="el-icon-edit" style="margin-right:10px">编辑</el-button>
          </router-link>
          <el-button @click="deletecate(scope.row.id)" size="small" type="danger" icon="el-icon-delete" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :current-page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { articleCateList, deleteCate, updataCate, changeState } from "@/api/article-cate";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination

export default {
  name: "ArticleList",
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageType: "A"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 删除分类
    deletecate(id) {
      let that = this
      this.$confirm('此操作将删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCate({
          id: id
        }).then(res => {
          if (res.success == true) {
            this.$notify({
              title: "删除成功",
              message: "文章分类删除成功",
              type: "success",
              duration: 2000
            });
            that.getList();
          }
        });
      }).catch(() => {
        return false;      
      });
    },
    getList() {
      this.listLoading = true;
      articleCateList(this.listQuery).then(response => {
        this.list = response.data;
        this.total = response.count;
        this.listLoading = false;
      });
    },
    switchChange(e, data) {
        changeState({id: data.id, hidden: data.hidden}).then(response => {

        });
    }
  }
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
